	<?php
/**
* Classe permettant de manipuler les bases de données
* Classe utilisant le design patern singleton 
* Accés depuis l'exterieur via la methode statique database::getInstance()
* 
*/
class DatabaseAdherent {

	private static $connection = null;

	/**
	* @desc : Constructeur, initialise la connection
	* @return void
	*/
	private function __construct()
	{
		try {
			// On instancie PDO et on se connecte a la base de données
			self::$connection = new PDO('mysql:host=localhost;dbname=m2l', 'root', '',  array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
		} catch(PDOException $e) {
			echo "Erreur de connection à la base de données : <b>".$e->getMessage()."</b>";
		}
	}

	/**
	* @desc : Methode statique d'accès à la database, retourne une instance de la connection si elle existe, en créer une si aucune existante
	* @return instance de database
	*/
	public static function getInstance() {
		if(self::$connection === null) {
			return new self();
		}
		return self::$connection;
	}

	public function infoAdherent($idAdherent) {

		$query = self::$connection->prepare("SELECT nom_adherent, prenom_adherent, adresse_adherent, ville_adherent, cp_adherent, certificat_adherent, sexe_adherent, date_inscription_adherent 
											 FROM adherent 
											 WHERE numero_licence = :numero_licence");
		
		$query->bindValue(":numero_licence", $idAdherent, PDO::PARAM_INT);

		$query->execute();

		if($query->fetchColumn() >= 1) {
			return $query->fetch(PDO::FETCH_OBJ);
		}

		return false;
	}

		public function listAdherent (){

			$query = self::$connection->prepare("SELECT nom_adherent, prenom_adherent, adresse_adherent, ville_adherent, cp_adherent, certificat_adherent, sexe_adherent, date_inscription_adherent 
											 	 FROM adherent
											 	 WHERE ");

			$query->execute();
			
			return $query->fetchAll(PDO::FETCH_OBJ);
	}

	public function checkLogin($login) {

		$query = self::$connection->prepare("SELECT count(*) FROM connexion WHERE mail_connexion = :mail_connexion");
		$query->bindValue(":mail_connexion", $login, PDO::PARAM_STR);

		$query->execute();

		if($query->fetchColumn() == 1) {	
			return true;
		}

		return false;

	}

	public function checkPassword($login, $password) {

		$query = self::$connection->prepare("SELECT count(*) FROM connexion WHERE password_connexion = :password_connexion AND mail_connexion = :mail_connexion");
		$query->bindValue(":password_connexion", $password, PDO::PARAM_STR);
		$query->bindValue(":mail_connexion", $login, PDO::PARAM_STR);

		$query->execute();

		if($query->fetchColumn() == 1) {	
			return true;
		}

		return false;

	}

	public function getIdAdherent($login){

		$query = self::$connection->prepare("SELECT numero_licence 
										 	 FROM adherent, connexion 
											 WHERE connexion.numero_connexion = adherent.connexion_numero_connexion 
											 AND mail_connexion = :login");
		$query->bindValue(":login", $login, PDO::PARAM_STR);
		$query->execute();
		$result = $query->fetch(PDO::FETCH_OBJ);

		// On débute une nouvelle session
		session_start();

		// On stock dans la session l'id de l'utilisateur
		$_SESSION["id"] = $result;
		return $result;
		
	}

	public function listReservationFromAdherant($idAdherant) {
		$query = self::$connection->prepare("SELECT * FROM reservation WHERE numero_licence = :numero_licence AND date >= now()");
		$query->bindValue(":numero_licence", $idAdherant, PDO::PARAM_INT);

		$query->execute();

		if($query->fetchColumn() > 0) {
			return $query->fetchAll(PDO::FETCH_OBJ);
		}
		else {
			return false;
		}
	}

	public function getIdAssoc($idAdherant) {
		$query = self::$connection->prepare("SELECT numero_association
											FROM association, association_has_adherent, adherent
											WHERE numero_licence = :idAdherant
											AND numero_licence = adherent_numero_licence
											AND association_numero_association = numero_association");
		$query->bindValue(":idAdherant", $idAdherant, PDO::PARAM_INT);

		$query->execute();

		return $query->fetch(PDO::FETCH_OBJ);
	}

}
?>